<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="周模式名称" prop="settingName">
        <el-input
          v-model="queryParams.settingName"
          placeholder="请输入周模式名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:settingufox:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:settingufox:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:settingufox:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:settingufox:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="settingufoxList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="周设置ID" align="center" prop="settingId" />
      <el-table-column label="周模式名称" align="center" prop="settingName" />
      <el-table-column label="周设置" align="center" prop="mondayEnabled" />
      <el-table-column label="主灯时段一启用" align="center" prop="masterTimespan1Enabled" />
      <el-table-column label="主灯时段一亮灯时间" align="center" prop="masterTimespan1OpenTime" />
      <el-table-column label="辅灯时段一启用" align="center" prop="secondryTimespan1Enabled" />
      <el-table-column label="辅灯时段一亮灯时间" align="center" prop="secondryTimespan1OpenTime" />
      <el-table-column label="调光" align="center" prop="adjust1Enabled" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:settingufox:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:settingufox:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改周模式设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="周模式名称" prop="settingName">
          <el-input v-model="form.settingName" placeholder="请输入周模式名称" />
        </el-form-item>
        <el-form-item label="企业ID" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入企业ID" />
        </el-form-item>
        <el-form-item label="项目ID" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目ID" />
        </el-form-item>
        <el-form-item label="周设置" prop="mondayEnabled">
          <el-input v-model="form.mondayEnabled" placeholder="请输入周设置" />
        </el-form-item>
        <el-form-item label="周二启用" prop="tuesdayEnabled">
          <el-input v-model="form.tuesdayEnabled" placeholder="请输入周二启用" />
        </el-form-item>
        <el-form-item label="周三启用" prop="wednesdayEnabled">
          <el-input v-model="form.wednesdayEnabled" placeholder="请输入周三启用" />
        </el-form-item>
        <el-form-item label="周四启用" prop="thursdayEnabled">
          <el-input v-model="form.thursdayEnabled" placeholder="请输入周四启用" />
        </el-form-item>
        <el-form-item label="周五启用" prop="fridayEnabled">
          <el-input v-model="form.fridayEnabled" placeholder="请输入周五启用" />
        </el-form-item>
        <el-form-item label="周六启用" prop="saturdayEnabled">
          <el-input v-model="form.saturdayEnabled" placeholder="请输入周六启用" />
        </el-form-item>
        <el-form-item label="周日启用" prop="sundayEnabled">
          <el-input v-model="form.sundayEnabled" placeholder="请输入周日启用" />
        </el-form-item>
        <el-form-item label="主灯时段一启用" prop="masterTimespan1Enabled">
          <el-input v-model="form.masterTimespan1Enabled" placeholder="请输入主灯时段一启用" />
        </el-form-item>
        <el-form-item label="主灯时段一亮灯时间" prop="masterTimespan1OpenTime">
          <el-input v-model="form.masterTimespan1OpenTime" placeholder="请输入主灯时段一亮灯时间" />
        </el-form-item>
        <el-form-item label="主灯时段一熄灯时间" prop="masterTimespan1CloseTime">
          <el-input v-model="form.masterTimespan1CloseTime" placeholder="请输入主灯时段一熄灯时间" />
        </el-form-item>
        <el-form-item label="主灯时段二启用" prop="masterTimespan2Enabled">
          <el-input v-model="form.masterTimespan2Enabled" placeholder="请输入主灯时段二启用" />
        </el-form-item>
        <el-form-item label="主灯时段二亮灯时间" prop="masterTimespan2OpenTime">
          <el-input v-model="form.masterTimespan2OpenTime" placeholder="请输入主灯时段二亮灯时间" />
        </el-form-item>
        <el-form-item label="主灯时段二熄灯时间" prop="masterTimespan2CloseTime">
          <el-input v-model="form.masterTimespan2CloseTime" placeholder="请输入主灯时段二熄灯时间" />
        </el-form-item>
        <el-form-item label="辅灯时段一启用" prop="secondryTimespan1Enabled">
          <el-input v-model="form.secondryTimespan1Enabled" placeholder="请输入辅灯时段一启用" />
        </el-form-item>
        <el-form-item label="辅灯时段一亮灯时间" prop="secondryTimespan1OpenTime">
          <el-input v-model="form.secondryTimespan1OpenTime" placeholder="请输入辅灯时段一亮灯时间" />
        </el-form-item>
        <el-form-item label="辅灯时段一熄灯时间" prop="secondryTimespan1CloseTime">
          <el-input v-model="form.secondryTimespan1CloseTime" placeholder="请输入辅灯时段一熄灯时间" />
        </el-form-item>
        <el-form-item label="辅灯时段二启用" prop="secondryTimespan2Enabled">
          <el-input v-model="form.secondryTimespan2Enabled" placeholder="请输入辅灯时段二启用" />
        </el-form-item>
        <el-form-item label="辅灯时段二亮灯时间" prop="secondryTimespan2OpenTime">
          <el-input v-model="form.secondryTimespan2OpenTime" placeholder="请输入辅灯时段二亮灯时间" />
        </el-form-item>
        <el-form-item label="辅灯时段二熄灯时间" prop="secondryTimespan2CloseTime">
          <el-input v-model="form.secondryTimespan2CloseTime" placeholder="请输入辅灯时段二熄灯时间" />
        </el-form-item>
        <el-form-item label="调光" prop="adjust1Enabled">
          <el-input v-model="form.adjust1Enabled" placeholder="请输入调光" />
        </el-form-item>
        <el-form-item label="调光1开始时间" prop="adjust1StartTime">
          <el-input v-model="form.adjust1StartTime" placeholder="请输入调光1开始时间" />
        </el-form-item>
        <el-form-item label="调光1截止时间" prop="adjust1EndTime">
          <el-input v-model="form.adjust1EndTime" placeholder="请输入调光1截止时间" />
        </el-form-item>
        <el-form-item label="调光1光强" prop="adjust1LightValue">
          <el-input v-model="form.adjust1LightValue" placeholder="请输入调光1光强" />
        </el-form-item>
        <el-form-item label="调光2启用" prop="adjust2Enabled">
          <el-input v-model="form.adjust2Enabled" placeholder="请输入调光2启用" />
        </el-form-item>
        <el-form-item label="调光2开始时间" prop="adjust2StartTime">
          <el-input v-model="form.adjust2StartTime" placeholder="请输入调光2开始时间" />
        </el-form-item>
        <el-form-item label="调光2截止时间" prop="adjust2EndTime">
          <el-input v-model="form.adjust2EndTime" placeholder="请输入调光2截止时间" />
        </el-form-item>
        <el-form-item label="调光2光强" prop="adjust2LightValue">
          <el-input v-model="form.adjust2LightValue" placeholder="请输入调光2光强" />
        </el-form-item>
        <el-form-item label="调光3启用" prop="adjust3Enabled">
          <el-input v-model="form.adjust3Enabled" placeholder="请输入调光3启用" />
        </el-form-item>
        <el-form-item label="调光3开始时间" prop="adjust3StartTime">
          <el-input v-model="form.adjust3StartTime" placeholder="请输入调光3开始时间" />
        </el-form-item>
        <el-form-item label="调光3截止时间" prop="adjust3EndTime">
          <el-input v-model="form.adjust3EndTime" placeholder="请输入调光3截止时间" />
        </el-form-item>
        <el-form-item label="调光3光强" prop="adjust3LightValue">
          <el-input v-model="form.adjust3LightValue" placeholder="请输入调光3光强" />
        </el-form-item>
        <el-form-item label="调光4启用" prop="adjust4Enabled">
          <el-input v-model="form.adjust4Enabled" placeholder="请输入调光4启用" />
        </el-form-item>
        <el-form-item label="调光4开始时间" prop="adjust4StartTime">
          <el-input v-model="form.adjust4StartTime" placeholder="请输入调光4开始时间" />
        </el-form-item>
        <el-form-item label="调光4截止时间" prop="adjust4EndTime">
          <el-input v-model="form.adjust4EndTime" placeholder="请输入调光4截止时间" />
        </el-form-item>
        <el-form-item label="调光4光强" prop="adjust4LightValue">
          <el-input v-model="form.adjust4LightValue" placeholder="请输入调光4光强" />
        </el-form-item>
        <el-form-item label="移动侦测时段1启用" prop="motionDetecTimespan1Enabled">
          <el-input v-model="form.motionDetecTimespan1Enabled" placeholder="请输入移动侦测时段1启用" />
        </el-form-item>
        <el-form-item label="移动侦测时段1开始时间" prop="motionDetecTimespan1StartTime">
          <el-input v-model="form.motionDetecTimespan1StartTime" placeholder="请输入移动侦测时段1开始时间" />
        </el-form-item>
        <el-form-item label="移动侦测时段1截止时间" prop="motionDetecTimespan1EndTime">
          <el-input v-model="form.motionDetecTimespan1EndTime" placeholder="请输入移动侦测时段1截止时间" />
        </el-form-item>
        <el-form-item label="移动侦测时段2启用" prop="motionDetecTimespan2Enabled">
          <el-input v-model="form.motionDetecTimespan2Enabled" placeholder="请输入移动侦测时段2启用" />
        </el-form-item>
        <el-form-item label="移动侦测时段2开始时间" prop="motionDetecTimespan2StartTime">
          <el-input v-model="form.motionDetecTimespan2StartTime" placeholder="请输入移动侦测时段2开始时间" />
        </el-form-item>
        <el-form-item label="移动侦测时段2截止时间" prop="motionDetecTimespan2EndTime">
          <el-input v-model="form.motionDetecTimespan2EndTime" placeholder="请输入移动侦测时段2截止时间" />
        </el-form-item>
        <el-form-item label="移动侦测相邻亮灯数" prop="motionDetecAdjacentLightCount">
          <el-input v-model="form.motionDetecAdjacentLightCount" placeholder="请输入移动侦测相邻亮灯数" />
        </el-form-item>
        <el-form-item label="移动侦测无目标光强" prop="motionDetecNoTargetLightIntensity">
          <el-input v-model="form.motionDetecNoTargetLightIntensity" placeholder="请输入移动侦测无目标光强" />
        </el-form-item>
        <el-form-item label="移动侦测延时亮灯" prop="motionDetecTurnOffDelay">
          <el-input v-model="form.motionDetecTurnOffDelay" placeholder="请输入移动侦测延时亮灯" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSettingufox, getSettingufox, delSettingufox, addSettingufox, updateSettingufox } from "@/api/ufox/settingufox";

export default {
  name: "Settingufox",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 周模式设置表格数据
      settingufoxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        settingName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyId: [
          { required: true, message: "企业ID不能为空", trigger: "blur" }
        ],
        projectId: [
          { required: true, message: "项目ID不能为空", trigger: "blur" }
        ],
        mondayEnabled: [
          { required: true, message: "周设置不能为空", trigger: "blur" }
        ],
        tuesdayEnabled: [
          { required: true, message: "周二启用不能为空", trigger: "blur" }
        ],
        wednesdayEnabled: [
          { required: true, message: "周三启用不能为空", trigger: "blur" }
        ],
        thursdayEnabled: [
          { required: true, message: "周四启用不能为空", trigger: "blur" }
        ],
        fridayEnabled: [
          { required: true, message: "周五启用不能为空", trigger: "blur" }
        ],
        saturdayEnabled: [
          { required: true, message: "周六启用不能为空", trigger: "blur" }
        ],
        sundayEnabled: [
          { required: true, message: "周日启用不能为空", trigger: "blur" }
        ],
        masterTimespan1Enabled: [
          { required: true, message: "主灯时段一启用不能为空", trigger: "blur" }
        ],
        masterTimespan2Enabled: [
          { required: true, message: "主灯时段二启用不能为空", trigger: "blur" }
        ],
        secondryTimespan1Enabled: [
          { required: true, message: "辅灯时段一启用不能为空", trigger: "blur" }
        ],
        secondryTimespan2Enabled: [
          { required: true, message: "辅灯时段二启用不能为空", trigger: "blur" }
        ],
        adjust1Enabled: [
          { required: true, message: "调光不能为空", trigger: "blur" }
        ],
        adjust2Enabled: [
          { required: true, message: "调光2启用不能为空", trigger: "blur" }
        ],
        adjust3Enabled: [
          { required: true, message: "调光3启用不能为空", trigger: "blur" }
        ],
        adjust4Enabled: [
          { required: true, message: "调光4启用不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询周模式设置列表 */
    getList() {
      this.loading = true;
      listSettingufox(this.queryParams).then(response => {
        this.settingufoxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        settingId: null,
        settingName: null,
        companyId: null,
        projectId: null,
        mondayEnabled: null,
        tuesdayEnabled: null,
        wednesdayEnabled: null,
        thursdayEnabled: null,
        fridayEnabled: null,
        saturdayEnabled: null,
        sundayEnabled: null,
        masterTimespan1Enabled: null,
        masterTimespan1OpenTime: null,
        masterTimespan1CloseTime: null,
        masterTimespan2Enabled: null,
        masterTimespan2OpenTime: null,
        masterTimespan2CloseTime: null,
        secondryTimespan1Enabled: null,
        secondryTimespan1OpenTime: null,
        secondryTimespan1CloseTime: null,
        secondryTimespan2Enabled: null,
        secondryTimespan2OpenTime: null,
        secondryTimespan2CloseTime: null,
        adjust1Enabled: null,
        adjust1StartTime: null,
        adjust1EndTime: null,
        adjust1LightValue: null,
        adjust2Enabled: null,
        adjust2StartTime: null,
        adjust2EndTime: null,
        adjust2LightValue: null,
        adjust3Enabled: null,
        adjust3StartTime: null,
        adjust3EndTime: null,
        adjust3LightValue: null,
        adjust4Enabled: null,
        adjust4StartTime: null,
        adjust4EndTime: null,
        adjust4LightValue: null,
        createTime: null,
        updateTime: null,
        motionDetecTimespan1Enabled: null,
        motionDetecTimespan1StartTime: null,
        motionDetecTimespan1EndTime: null,
        motionDetecTimespan2Enabled: null,
        motionDetecTimespan2StartTime: null,
        motionDetecTimespan2EndTime: null,
        motionDetecAdjacentLightCount: null,
        motionDetecNoTargetLightIntensity: null,
        motionDetecTurnOffDelay: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.settingId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加周模式设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const settingId = row.settingId || this.ids
      getSettingufox(settingId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改周模式设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.settingId != null) {
            updateSettingufox(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSettingufox(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const settingIds = row.settingId || this.ids;
      this.$modal.confirm('是否确认删除周模式设置编号为"' + settingIds + '"的数据项？').then(function() {
        return delSettingufox(settingIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/settingufox/export', {
        ...this.queryParams
      }, `settingufox_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
